using System;
using BitMango.Diagnostics;
using UnityEngine;

public static class Sound
{
    public static void Play(string soundName, float volume = 1f, float pitch = 1f, bool loop = false)
    {
        if (!Sound.enableSFX)
        {
            return;
        }
        Debugger.Assert(SingletonMonoBehaviour<SoundManager>.Instance != null, "SoundManager : Play - SoundManager Instance is null");
        SingletonMonoBehaviour<SoundManager>.Instance.Play(soundName, volume, pitch, loop);
    }

    public static void PlayBGM(string soundName, float volume = 1f, int channel = 0, bool loop = true, bool useFade = true)
    {
        if (!Sound.enableBGM)
        {
            return;
        }
        Debugger.Assert(SingletonMonoBehaviour<SoundManager>.Instance != null, "SoundManager : PlayBGM - SoundManager Instance is null");
        SingletonMonoBehaviour<SoundManager>.Instance.PlayBGM(soundName, volume, channel, loop, useFade);
    }

    public static void StopByName(string soundName)
    {
        Debugger.Assert(SingletonMonoBehaviour<SoundManager>.Instance != null, "SoundManager : StopByName - SoundManager Instance is null");
        SingletonMonoBehaviour<SoundManager>.Instance.Stop(soundName);
    }

    public static void StopBGM()
    {
        Debugger.Assert(SingletonMonoBehaviour<SoundManager>.Instance != null, "SoundManager : StopBGM - SoundManager Instance is null");
        SingletonMonoBehaviour<SoundManager>.Instance.StopBGM();
    }

    public static bool IsPlaySound(string soundName)
    {
        Debugger.Assert(SingletonMonoBehaviour<SoundManager>.Instance != null, "SoundManager : IsPlaySound - SoundManager Instance is null");
        return SingletonMonoBehaviour<SoundManager>.Instance.IsPlaySound(soundName);
    }

    public static void Stop(string soundName)
    {
        Debugger.Assert(SingletonMonoBehaviour<SoundManager>.Instance != null, "SoundManager : Stop - SoundManager Instance is null");
        SingletonMonoBehaviour<SoundManager>.Instance.Stop(soundName);
    }

    public static void SetVolume(float vol)
    {
        AudioListener.volume = vol;
    }

    public static void SetBGMVolume(float volume)
    {
        Debugger.Assert(SingletonMonoBehaviour<SoundManager>.Instance != null, "SoundManager : SetBGMVolume - SoundManager Instance is null");
        SingletonMonoBehaviour<SoundManager>.Instance.SetBGMVolume(volume);
    }

    public static bool EnableBGM
    {
        get
        {
            return Sound.enableBGM;
        }
        set
        {
            Sound.enableBGM = value;
            if (!Sound.enableBGM)
            {
                Sound.StopBGM();
            }
            PlayerPrefs.SetInt("Sound.enableBGM", (!Sound.enableBGM) ? 0 : 1);
            PlayerPrefs.Save();
        }
    }

    public static bool EnableSFX
    {
        get
        {
            return Sound.enableSFX;
        }
        set
        {
            Sound.enableSFX = value;
            PlayerPrefs.SetInt("Sound.enableSFX", (!Sound.enableSFX) ? 0 : 1);
            PlayerPrefs.Save();
        }
    }

    private static bool enableSFX = PlayerPrefs.GetInt("Sound.enableSFX", 1) == 1;

    private static bool enableBGM = PlayerPrefs.GetInt("Sound.enableBGM", 1) == 1;
}
